- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathleft view of tree.cpp
61 lines (47 loc) · 1.02 KB
/
left view of tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// C++ program to print left view of Binary Tree
#include<bits/stdc++.h>
usingnamespacestd;
structNode
{
int data;
structNode *left, *right;
};
structNode *newNode(int item)
{
structNode *temp = (structNode *)malloc(
sizeof(structNode));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
// left view of a binary tree.
voidleftViewUtil(structNode *root,
int level, int *max_level)
{
if (root == NULL) return;
if (*max_level < level)
{
cout << root->data << "";
*max_level = level;
}
leftViewUtil(root->left, level + 1, max_level);
leftViewUtil(root->right, level + 1, max_level);
}
voidleftView(structNode *root)
{
int max_level = 0;
leftViewUtil(root, 1, &max_level);
}
intmain()
{
Node* root = newNode(10);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(7);
root->left->right = newNode(8);
root->right->right = newNode(15);
root->right->left = newNode(12);
root->right->right->left = newNode(14);
leftView(root);
return0;
}